﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WCFServiceWebRole1
{
    // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼、svc 和組態檔中的類別名稱 "Service1"。
    // 注意: 若要啟動 WCF 測試用戶端以便測試此服務，請在 [方案總管] 中選取 Service1.svc 或 Service1.svc.cs，然後開始偵錯。
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        MedicareDBDataContext db = new MedicareDBDataContext();
        public IEnumerable<MyStaff> GetStaffs()
        {

            var q = from p in db.Staff
                    select new MyStaff
                    {
                        ID = p.Staff_ID,
                        Name = p.Staff_Name,
                        Div = p.Division.Div_Name,
                        Tel = p.Staff_TN,
                        Photo = IsPhotoNull(p.Staff_Pix) ? p.Staff_Pix.ToArray() : null,
                        Job = p.JobTitle.Job_Title,
                        Edu = p.Staff_Edu,
                        Exp = p.Staff_Exp,
                        Skill = p.Staff_Skill,
                        Score = isScoreNull(p.Evaluations.Where(n => n.Staff_ID == p.Staff_ID).Average(n => (double)n.Eva_Sco)),
                        NewComment = p.Evaluations.Where(n => n.Staff_ID == p.Staff_ID).Max(n => n.Eva_Com)
                    };
            return q.AsEnumerable();
        }

        private double isScoreNull(double? x)
        {
            if (x == null)
            {
                return 0;
            }
            return x.Value;
        }

        //判斷圖片是否NULL
        private bool IsPhotoNull(Binary picbyte)
        {
            if (picbyte == null || picbyte.Length == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //查詢今日應提醒之病患 
        public IEnumerable<DrugRemind> GetReminds(DateTime date, int PatientID)
        {
            var q = from p in db.View_DrugReminder
                    where (p.PatientID == PatientID &&  //只查詢該病患
                    p.DrugTakenDate.Date <= date.Date.AddDays(p.BeforeDays) && //只查詢該提醒的 
                    p.DrugTakenDate >= date)  //查詢某日期之後的 (通常用今天)
                    orderby p.DrugTakenDate ascending
                    select new DrugRemind
                    {
                        PatientName = p.PatientName,
                        EventTime = p.DrugTakenDate,
                        EventName = p.DrugName,
                        Division = p.Division,
                        //PhoneNumber = p.PhoneNum,
                        //Email = p.Email,
                        WantBeRemind = p.WantRemind,
                        CalendarID = p.DrugCalendarID,
                        IsRemind = p.isRemind,
                        BeforeDays = p.BeforeDays
                    };
            return q.AsEnumerable();
        }

        //病患輸入姓名、身分證之後得到他的ID
        public int GetPatientID(string IDentity, string PatientName)
        {
            var q = db.Patient.Where(n => n.Ptt_Name == PatientName && n.Ptt_PID == IDentity).Single();
            return q.Ptt_ID;
        }
    }
}
